<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body>
    <!--  非父子组件传值     1  事件总线  2  vuex  -->
    <div id="app">
      <h1>{{msg}}</h1>

        <home></home>

        <news></news>
    </div>

    <template id="homes">
      <div>
        <h2>{{msg}} ---  接受news传递过来的值 :  {{bbb}}</h2>
        <button  @click='sendToBus'>点击发送到事件总线身上 去 news</button>  
       </div> 
        
    </template>

    <template id="news">
      
      <div>
      <h2>{{msg}}-----接受home传递过来的值 :  {{aaa}}</h2>
      <button  @click='sendToBus'>点击发送到事件总线身上 去 home </button>  
      </div> 
      
      
    </template>
   
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <script>

      //    事件总线  
      var eventBus = new Vue({

      })

      let home = {
            data() {
                return {
                    msg: 'home',
                    bbb: ''
                }
            },
            template: '#homes',
            methods: {
                sendToBus() {

                    eventBus.$emit('home-send', this.msg)

                }
            },
            mounted() {

                eventBus.$on('第一个参数是发送过来的事件名字', '第二个是一个回调函数 回调函数的参数就是传递过来的值')
                //  接受传递过来的方法以及数据 

                eventBus.$on('news-send', (data) => {
                    console.log(data)
                    this.bbb = data
                })
            },
        }

        let news = {

data() {
    return {
        msg: 'news',
        aaa: ''
    }
},
template:'#news',
methods: {
    sendToBus() {
        //  事件总线发送  
        eventBus.$emit('news-send', this.msg)


    }
},
mounted() {
    eventBus.$on('home-send', (data) => {
        console.log(data)
        this.aaa = data
    })
},
}



      var vm = new Vue({
        el: "#app",
        data: {
          msg: "vue",
        },
        methods: {},
        components:{
          news,
          home
        }
          
        
      });
    </script>
  </body>
</html>
